package com.xiebishe.mylibrary.common.groupAdapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public abstract class RecyclerOneAdapter<T,VH extends RecyclerOneAdapter.ViewHolder<T>> extends RecyclerAdapter<RecyclerOneAdapter.MyHolder<T,VH>> {
    private T data;

    public RecyclerOneAdapter(T data) {
        this.data = data;
    }

    public final void setData(T data) {
        this.data = data;
        notifyDataSetChanged();
    }

    protected abstract void bindToView(VH holder);

    protected abstract VH createViewHolder(LayoutInflater inflater, ViewGroup parent);

    @Override
    public final int getItemCount() {
        return 1;
    }

    @NonNull
    @Override
    public final MyHolder<T,VH>  onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater from = LayoutInflater.from(parent.getContext());
        VH viewHolder = createViewHolder(from, parent);
        MyHolder<T,VH>  myHolder = new MyHolder<>(viewHolder.getItemView());
        myHolder.setHolder(viewHolder);
        return myHolder;
    }

    @Override
    public final void onBindViewHolder(@NonNull MyHolder<T,VH> holder, int position) {
        holder.getHolder().setData(data);
        holder.getHolder().setMyHolder(holder);
        bindToView(holder.getHolder());
    }

    @Override
    public final void onBindViewHolder(@NonNull MyHolder<T,VH>  holder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
    }


    @Override
    public final int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public final void setHasStableIds(boolean hasStableIds) {
        super.setHasStableIds(hasStableIds);
    }

    @Override
    public final long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public final void onViewRecycled(@NonNull MyHolder<T,VH> holder) {
        super.onViewRecycled(holder);
    }

    @Override
    public final boolean onFailedToRecycleView(@NonNull MyHolder<T,VH> holder) {
        return super.onFailedToRecycleView(holder);
    }

    @Override
    public final void onViewAttachedToWindow(@NonNull MyHolder<T,VH> holder) {
        super.onViewAttachedToWindow(holder);
    }

    @Override
    public final void onViewDetachedFromWindow(@NonNull MyHolder<T,VH> holder) {
        super.onViewDetachedFromWindow(holder);
    }

    @Override
    public final void registerAdapterDataObserver(@NonNull RecyclerView.AdapterDataObserver observer) {
        super.registerAdapterDataObserver(observer);
    }

    @Override
    public final void unregisterAdapterDataObserver(@NonNull RecyclerView.AdapterDataObserver observer) {
        super.unregisterAdapterDataObserver(observer);
    }

    @Override
    public final void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public final void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
    }



    public static abstract class ViewHolder<T> {
        private final View itemView;
        private T data;
        private MyHolder<T,? extends ViewHolder<T>> myHolder;
        protected ViewHolder(View itemView) {
            this.itemView = itemView;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }
        View getItemView(){
            return itemView;
        }

        public MyHolder<T,? extends ViewHolder<T>> getMyHolder() {
            return myHolder;
        }

        void setMyHolder(MyHolder<T,? extends ViewHolder<T>> myHolder) {
            this.myHolder = myHolder;
        }
    }

    public static class MyHolder<T,VH extends ViewHolder<T>> extends RecyclerView.ViewHolder {
        VH holder;

        MyHolder(View itemView) {
            super(itemView);
        }

        public VH getHolder() {
            return holder;
        }

        public void setHolder(VH holder) {
            this.holder = holder;
        }
    }
}
